package com.fastCms.business.frame.utils;

import com.alibaba.fastjson.JSON;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class BeanConvertUtil {

    /**
     * 对象转换
     *
     * @param <V>
     * @param bean
     * @param clazz
     * @return
     */
    public static <V> V convertBean(Object bean, Class<V> clazz) {
        if (bean == null) {
            return null;
        }
        V v = null;
        try {
            v = clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
            return null;
        }
        BeanUtils.copyProperties(bean, v);
        return v;
    }

    /**
     * 列表转换
     *
     * @param <V>
     * @param list
     * @param clazz
     * @return
     */
    public static <V> List<V> convertList(List<?> list, Class<V> clazz) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<V> result = list.stream().map(t -> {
            return convertBean(t, clazz);
        }).collect(Collectors.toList());
        return result;
    }

    /**
     * list转Map
     *
     * @param <T>
     * @param <K>
     * @param <V>
     * @param c
     * @param keyMapper
     * @param valMapper
     * @return
     */
    public static <T, K, V> Map<K, V> convertList2Map(Collection<T> list, Function<? super T, ? extends K> keyMapper,
                                                      Function<? super T, ? extends V> valMapper) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }
        Map<K, V> map = list.stream().collect(Collectors.toMap(keyMapper, valMapper));
        return map;
    }

    /**
     * list转value为List的Map
     *
     * @param <T>
     * @param <V>
     * @param list
     * @param keyMapper
     * @return
     */
    public static <T, K> Map<K, List<T>> convertList2ListMap(Collection<T> list,
                                                             Function<? super T, ? extends K> keyMapper) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }
        Map<K, List<T>> map = list.stream().collect(Collectors.groupingBy(keyMapper));
        return map;
    }

    /**
     * 处理图片json
     *
     * @param images
     * @return
     */
    public static List<String> converImages2List(String images) {
        // 解析图片json
        if (StringUtils.isNotBlank(images)) {
            List<String> imageUrls = JSON.parseArray(images, String.class);
            return imageUrls;
        }
        return Collections.emptyList();
    }

    /**
     * 对象转换(Map key下划线大写)
     *
     * @param <V>
     * @param beanMap
     * @param clazz
     * @return
     */
    public static <V> V convertBeanByMapUpperKey(Map<String, String> beanMap, Class<V> clazz) {
        try {
            V v = clazz.newInstance();
            BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
            PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor prop : properties) {
                //驼峰转下划线大写
                String key = prop.getName().replaceAll("[A-Z]", "_$0").toUpperCase();
                if (beanMap.containsKey(key) && Objects.nonNull(beanMap.get(key))) {
                    Object value = beanMap.get(key);
                    Method setMethod = prop.getWriteMethod();
                    setMethod.invoke(v, value);
                }
            }
            return v;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
